<?php
namespace app\adm\controller;
use think\Controller;
use think\Db;
use think\Request;

//后台话题管理
class Huati extends Index{
	public function __construct(Request $request){
		parent::__construct($request);
	}
	//话题列表
	public function huatilist(){
		$page = Db::name('huati')->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$huatilist = $page->all();
		foreach ($huatilist as $key => $value) {
			$huatilist[$key]['nickname']=Db::name('user')->where(['userid'=>$huatilist[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('huatilist',$huatilist);
		return $this->fetch('huatilist',['title'=>'话题列表']);
	}
	//删除话题
	public function del_huati(){
		$huatiid = input('huatiid');
		$res = Db::name('huati')->delete($huatiid);
		// dump($res);die;
		if($res){
			jsBack('删除成功');
		}else{
			jsBack('删除失败');
		}
	}
	//审核话题
	public function shenhe(){
		$huatiid = input('huatiid');
		$sh = Db::name('huati')->where(['huatiid'=>$huatiid])->update(['huatitype'=>'1']);
		// dump($sh);die;
		if($sh){
			$this->success('审核成功');
		}else{
			$this->error('审核失败');
		}
	}
	/**
	* 发表后台话题
	*/
	public function huatiadd(){
		return $this->fetch('huatiadd',['title'=>'公共话题添加']);
	}
	public function huatiinsert(){
		$file = request()->file('huatiphoto');
		if($file){
			$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
			if($info){
				$data['huatiphoto']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
		        $data['huaticontent']=input('post.huaticontent');
				$data['huatititle']=input('post.huatititle');
				$data['addtime']=time();
				$data['userid']=session('userid');
				$data['huatitype']='1';
				$huati=Db::name('huati')->insertGetId($data);
				//添加相册
				if($huati){
					jsBack('上传成功','/adm/huati/huatilist');
				}else{
					jsBack('上传失败');
					return false;
				}
			}else{
				jsBack('内部错误');
			}
		}else{
			jsBack('请选择图片');
			return false;
		}
	}
}